/*************************************************************************
** decay plot to compare flow vs stock measures (Figure A1)
*************************************************************************/


*** collection
* aggregate collection accounts by status and time
use "${dataRoot}/coll_decay_200906", clear
foreach i of numlist 1/16{
	gen exist`i' = coll_status`i'==1
	gen die`i' = coll_status`i'==99
	gen paid`i' = coll_status`i'==2
	gen gone`i' = coll_status`i'==0
	drop coll_status`i'
}

drop subjectKey
gen tt = 1
collapse (mean) e* d* p* g*, by(tt) fast

reshape long exist die paid gone, i(tt) j(period)
drop tt
save "${dataRoot}/coll_decay_reshaped", replace


* generate area plot
use "${dataRoot}/coll_decay_reshaped", clear
replace paid = gone + die + paid
local newn = _N+1
set obs `newn'
replace period = 0 if period==.
replace paid   = 0 if paid==.
gen      base = 0
replace exist = 1
sort period
replace period = period*0.5

twoway (rarea paid base period, msize(small) color(blue*.6)) ///
			 (rarea exist paid period, msize(small) color(orange*0.7)), ///
			 graphregion(color(white)) bgcolor(white) ///
			 xlabel(0(1)8) xtitle("Years After June 2009") ///
			 legend(lab(1 "Collection Paid Out / Dropped") lab(2 "Collection Not Paid Out") ///
		   size(small) order(2 1) symxsize(*0.5) region(lwidth(none))) 
graph export "${figGeoDebt}/coll_decay_clean.pdf", replace




*** credit card
* aggregate collection accounts by status and time
use "${dataRoot}/cc_decay_200906", clear
foreach i of numlist 1/16{
	gen current`i' = cc_status`i'=="1"
	gen die`i' = cc_status`i'=="99"
	gen noncurrent`i' = cc_status`i'=="2"
	gen gone`i' = cc_status`i'=="0"
	gen missing`i' = cc_status`i'==""|cc_status`i'=="77"
	gen bkcy`i' = regexm(cc_status`i',"(UR_).*")
	gen paid`i' = current`i'==0 & die`i'==0 & noncurrent`i'==0 & gone`i'==0 & missing`i'==0 & bkcy`i'==0
	drop cc_status`i'
}

drop subjectKey
gen tt = 1
collapse (mean) c* d* n* g* m* b* p*, by(tt) fast

reshape long current noncurrent die gone missing bkcy paid, i(tt) j(period)
drop tt
save "${dataRoot}/cc_decay_reshaped", replace


* generate area plot
use "${dataRoot}/cc_decay_reshaped", clear
replace current = current + paid + bkcy + missing + gone + die
local newn = _N+1
set obs `newn'
replace period  = 0 if period==.
replace current = 0 if current==.
gen           base = 0
replace noncurrent = 1
sort period
replace period = period*0.5


twoway  (rarea current base period, color(blue*.6)) ///
			 (rarea noncurrent current period, color(orange*0.6)), ///
			 graphregion(color(white)) bgcolor(white) ///
			 xlabel(0(1)8) xtitle("Years After June 2009") ///
			 legend(lab(1 "Current, Paid Out, or Dropped") lab(2 "Delinquent") ///
		   size(small) rows(1) order(2 1) symxsize(*0.5) region(lwidth(none)))
graph export "${figGeoDebt}/cc_decay_clean.pdf", replace 
